#ifndef LEXICAL_CAST_HPP
#define LEXICAL_CAST_HPP

#include <string>

namespace sh
{

/**
 * 模板定义
 */
template<typename Target, typename Source>
struct converter
{
};

/**
 * 字符串和数字的转换，统一接口，只需要指定返回类型即可，但是整型类数字只能转换10进制，且无法知道是否转换成功
 */
template<typename Target>
Target lexical_cast(const std::string &arg)
{
    return converter<Target, std::string>::convert(arg);
}
template<typename Target>
Target lexical_cast(const char *arg)
{
    return converter<Target, std::string>::convert(arg);
}

/**
 * std::string => int
 */
template<typename Source>
struct converter<int, Source>
{
    static inline int convert(const std::string &source)
    { return std::atoi(source.c_str()); }
    static inline int convert(const char *source)
    { return std::atoi(source); }
};

/**
 * std::string => long
 */
template<typename Source>
struct converter<long, Source>
{
    static inline long convert(const std::string &source)
    { return std::atol(source.c_str()); }
    static inline long convert(const char *source)
    { return std::atol(source); }
};

/**
 * std::string => long long
 */
template<typename Source>
struct converter<long long, Source>
{
    static inline long long convert(const std::string &source)
    { return std::atoll(source.c_str()); }
    static inline long long convert(const char *source)
    { return std::atoll(source); }
};

/**
 * std::string => double
 */
template<typename Source>
struct converter<double, Source>
{
    static inline double convert(const std::string &source)
    { return std::atof(source.c_str()); }
    static inline double convert(const char *source)
    { return std::atof(source); }
};

}

#endif //LEXICAL_CAST_HPP
